# coding:utf-8
# 从照片中识别出脸部特征并将脸部图像保存下来
from PIL import Image
import face_recognition
import os, shutil
import os.path
import random
import time
import glob
from face_recognition.face_recognition_cli import image_files_in_folder

train_dir = "E:\face_login\app\pic"

save_path = "E:\face_login\app\face_pic"
# for class_dir in os.listdir(train_dir):
#     print(class_dir)

#     if not os.path.isdir(os.path.join(train_dir, class_dir)):
#         continue

# Loop through each training image for the current person
data_dir = train_dir + '/*'
files_list = glob.glob(data_dir)
for img_path in files_list:
    image = face_recognition.load_image_file(img_path)
    face_locations = face_recognition.face_locations(image)
    for face_location in face_locations:
        t = time.time()
        fice_name = random.randint(0, 10000)
        top, right, bottom, left = face_location
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        file_name = save_path + str(round(t * 1000)) + 'face' + str(fice_name) + '.png'
        pil_image.save(open(file_name, 'wb'), 'png')
